<!-- TabBar.vue -->
<template>
  <view class="tab-bar-container">
    <view class="tab-bar" :style="{ paddingBottom: 'env(safe-area-inset-bottom)' }">
      <view 
        v-for="(tab, index) in tabs" 
        :key="index" 
        :class="['tab-item', currentTab === index ? 'active' : '']" 
        @tap="switchTab(index)"
      >
        <view class="icon-box">
          <view class="tab-icon-bg" v-if="currentTab === index"></view>
          <text class="tab-icon">{{ getTabIcon(index) }}</text>
        </view>
        <text class="tab-text">{{ tab.text }}</text>
      </view>
    </view>
  </view>
</template>
  
<script setup>
import { ref } from 'vue'

const props = defineProps({
  currentTab: {
    type: Number,
    default: 0
  }
})

const emit = defineEmits(['switch-tab'])

// 底部导航配置
const tabs = [
  {
    text: '对话',
    icon: 'message'
  },
  {
    text: '发现',
    icon: 'search'
  },
  {
    text: '设备',
    icon: 'device' 
  },
  {
    text: '我的',
    icon: 'user'
  }
]

// 提供备用图标，防止字体图标失败
const getTabIcon = (index) => {
  const fallbackIcons = ['💬', '🔍', '🖥️', '👤']
  return fallbackIcons[index] || '•'
}

// 切换标签页
const switchTab = (index) => {
  const routes = [
    '/pages/index/index',
    '/pages/discover/discover',
    '/pages/device-manage/device-manage',
    '/pages/my/my'
  ]
  
  try {
    uni.switchTab({
      url: routes[index],
      fail: (err) => {
        console.error('标签导航失败:', err)
        // 如果switchTab失败，尝试使用navigateTo
        uni.navigateTo({
          url: routes[index]
        })
      }
    })
  } catch (error) {
    console.error('标签导航错误:', error)
  }
  
  emit('switch-tab', index)
}
</script>

<style lang="scss">
.tab-bar-container {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 100;
  
  .tab-bar {
    height: 110rpx;
    background: #fff;
    display: flex;
    justify-content: space-around;
    box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.05);
    border-top-left-radius: 24rpx;
    border-top-right-radius: 24rpx;
    
    .tab-item {
      flex: 1;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      position: relative;
      
      .icon-box {
        position: relative;
        width: 80rpx;
        height: 50rpx;
        display: flex;
        align-items: center;
        justify-content: center;
        
        .tab-icon-bg {
          position: absolute;
          width: 48rpx;
          height: 48rpx;
          border-radius: 24rpx;
          background: rgba(86, 119, 252, 0.1);
          z-index: 1;
          transform: scale(0);
          animation: icon-pop 0.3s ease forwards;
        }
        
        .tab-icon {
          position: relative;
          z-index: 2;
          font-size: 44rpx;
          color: #999;
          transition: all 0.3s ease;
        }
      }
      
      .tab-text {
        font-size: 24rpx;
        margin-top: 4rpx;
        color: #999;
        transition: all 0.3s ease;
      }
      
      &.active {
        .tab-icon {
          color: #5677fc;
          transform: translateY(-2rpx) scale(1.1);
        }
        
        .tab-text {
          color: #5677fc;
          font-weight: 500;
        }
      }
      
      &::after {
        content: '';
        position: absolute;
        bottom: 10rpx;
        left: 50%;
        width: 0;
        height: 4rpx;
        border-radius: 2rpx;
        background: #5677fc;
        transform: translateX(-50%);
        transition: width 0.3s ease;
      }
      
      &.active::after {
        width: 20rpx;
      }
    }
  }
}

@keyframes icon-pop {
  0% {
    transform: scale(0);
  }
  70% {
    transform: scale(1.2);
  }
  100% {
    transform: scale(1);
  }
}
</style>